package com.cb.cmsservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.cmsservice.entity.CrmBanner;
import com.cb.cmsservice.service.CrmBannerService;
import com.cb.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author Cebb
 * @since 2020-06-26
 */
@Api(description = "首页Banner管理(Admin)")

@RestController
@RequestMapping("/cmsservice/banneradmin")
public class BannerAdminController {

    private final CrmBannerService bannerService;

    @Autowired
    public BannerAdminController(CrmBannerService bannerService) {
        this.bannerService = bannerService;
    }

    @ApiOperation("分页查询banner")
    @GetMapping("pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable("current") long current,
                        @PathVariable("limit") long limit) {
        Page<CrmBanner> pageBanner = new Page<>(current, limit);
        IPage<CrmBanner> page = bannerService.page(pageBanner, null);

        return R.ok()
                .data("items", page.getRecords())
                .data("total", page.getTotal());
    }

    @ApiOperation("添加banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);

        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }

}

